//
//  WJGifView.m
//  WJBSApp
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 王景伟. All rights reserved.
//

// 动态图需要写圆圈圈  我计划卸载大Cell里面 

#import "WJGifView.h"
#import "DALabeledCircularProgressView.h"
#import "WJEssAndNewModel.h"
#import "WJShowPictureVC.h"
#import "WJDataManager.h"


@interface WJGifView ()

//  加载的圆形宽
@property (nonatomic,strong) DALabeledCircularProgressView *progressView;

@end

@implementation WJGifView

-(void)dealloc
{
    [_model release];
    [_progressView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
        
    }
    return self;
}


- (void)createView
{
    
    // 取消自动收缩功能
    self.autoresizingMask = UIViewAutoresizingNone;
    
    // 这个是圆圈圈
    self.progressView = [[DALabeledCircularProgressView alloc] init];
    // 头变成圆的
    self.progressView.roundedCorners = YES;
    self.progressView.progressTintColor = [UIColor orangeColor];
    [self addSubview:self.progressView];
    self.progressView.hidden = YES;
    [_progressView release];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];                 // 给视图添加手势  添加手指
    [tap release];

    
}


- (void)setModel:(WJEssAndNewModel *)model
{
    if (_model !=model) {
        [_model release];
        _model = [model retain];
    }
    
    [self sd_setImageWithURL:[NSURL URLWithString:self.model.gifModel.images[0]] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        if ([[WJDataManager shareFMDB] isExistPicName:self.model.gifModel.images[0]]) {
            
            self.progressView.hidden = YES;
            
        }else{
            
            self.progressView.hidden = NO;
            
            CGFloat proges = 1.0 * receivedSize / expectedSize;
            
            // 显示文字
            NSString *text = [NSString stringWithFormat:@"%.0f%%", proges * 100];
            self.progressView.progressLabel.text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
            
            //显示进度值
            [self.progressView setProgress:proges animated:NO];
        }
        
        
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        self.progressView.hidden = YES;
        
        if (![[WJDataManager shareFMDB] isExistPicName:self.model.gifModel.images[0]]) {
            
            [[WJDataManager shareFMDB] insertPicName:self.model.gifModel.images[0]];
        }
        
    }];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.progressView.frame = CGRectMake(self.bounds.size.width / 2 - 30, self.bounds.size.height / 2 - 30, 60, 60);

}





-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    WJShowPictureVC *vc = [[WJShowPictureVC alloc]init];
    
    vc.model = self.model;
    
    // 主屏显示
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
    
     NSLog(@"点了GIF");
}



















@end
